package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.ReplyLikeMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.ReplyLike;
import com.aaa.forum.services.IArticleReplyService;
import com.aaa.forum.services.IReplyLikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class ReplyLikeServiceImpl implements IReplyLikeService {

    @Resource
    private ReplyLikeMapper replyLikeMapper;

    @Resource
    private IArticleReplyService articleReplyService;

    @Override
    public void like(Long userId, Long replyId) {
        if (userId == null || userId <= 0 || replyId == null || replyId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询点赞记录是否存在
        ReplyLike replyLike = replyLikeMapper.selectByUserIdAndArticleId(userId, replyId);
        int row;
        if (replyLike == null) {
            // 不存在记录, insert 一条新记录
            replyLike = new ReplyLike();
            replyLike.setUserId(userId);
            replyLike.setReplyId(replyId);
            replyLike.setState((byte) 1);
            replyLike.setDeleteState((byte) 0);
            Date date = new Date();
            replyLike.setCreateTime(date);
            replyLike.setUpdateTime(date);
            row = replyLikeMapper.insertSelective(replyLike);
        } else {
            // 已经存在记录, 更新记录
            ReplyLike updateObject = new ReplyLike();
            updateObject.setId(replyLike.getId());
            updateObject.setState((byte) 1);
            updateObject.setDeleteState((byte) 0);
            updateObject.setUpdateTime(new Date());
            row = replyLikeMapper.updateByPrimaryKeySelective(updateObject);
        }
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        articleReplyService.addOneLikeCountById(replyId);
    }

    @Override
    public void cancel(Long userId, Long replyId) {
        if (userId == null || userId <= 0 || replyId == null || replyId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询点赞记录是否存在
        ReplyLike replyLike = replyLikeMapper.selectByUserIdAndArticleId(userId, replyId);
        if (replyLike == null || replyLike.getDeleteState() == 1 || replyLike.getState() == 0) {
            /*
                记录不存在, 无法取消点赞
                或者已经取消点赞了
             */
            return;
        }
        // 构造更新对象
        ReplyLike updateObject = new ReplyLike();
        updateObject.setId(replyLike.getId());
        updateObject.setState((byte) 0);
        updateObject.setUpdateTime(new Date());
        int row = replyLikeMapper.updateByPrimaryKeySelective(updateObject);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        articleReplyService.subOneLikeCountById(replyId);
    }
}